package com.zwq.wxlogin;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URI;

public class WXLoginRequest {

    public static String requestGetOne(String appid, String secret) throws Exception {
        BasicHttpClientConnectionManager connManager = null;
        if (false) {
        }
        else {
            connManager = new BasicHttpClientConnectionManager(
                    RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
                            .register("https", SSLConnectionSocketFactory.getSocketFactory())
                            .build(),
                    null,
                    null,
                    null
            );
        }

        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(connManager)
                .build();

        String url2 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;

        if (StringUtils.isEmpty(appid) || StringUtils.isEmpty(secret)) {
            return "error";
        }

        HttpGet httpGet = new HttpGet();
        URI uri = URI.create(url2);
        httpGet.setURI(uri);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(300).build();
        httpGet.setConfig(requestConfig);

//        HttpPost httpPost = new HttpPost(url);
//        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();
//        httpPost.setConfig(requestConfig);
//
//        StringEntity postEntity = new StringEntity(data, "UTF-8");
//        httpPost.addHeader("Content-Type", "text/xml");
////      httpPost.addHeader("User-Agent", USER_AGENT + " " + config.getMchID());
//        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        System.out.println("httpResponse : " + httpResponse.toString());
        HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity, "UTF-8");

    }


    /**
     * @Description: 定时更新某个服务号的 accessToken
     * @Author: mac
     * @Date: 2024/9/22 18:30
     * @param appid:
     * @param secret:
     * @param code:
     * @param url:
     * @return: org.apache.http.HttpEntity
     *
     */
    public static HttpEntity requestGetOne(String appid, String secret, String code, String url) throws Exception {
        BasicHttpClientConnectionManager connManager = null;
        if (false) {
        }
        else {
            connManager = new BasicHttpClientConnectionManager(
                    RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
                            .register("https", SSLConnectionSocketFactory.getSocketFactory())
                            .build(),
                    null,
                    null,
                    null
            );
        }

        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(connManager)
                .build();
//
        appid = "";
        secret = "";
//        String code = "";
//        // 通过CODE获取用户信息
//        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
//        // 获取 ACCESS_TOKEN
        String url2 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;

        HttpGet httpGet = new HttpGet();
        URI uri = URI.create(url2);
        httpGet.setURI(uri);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();
        httpGet.setConfig(requestConfig);

//        HttpPost httpPost = new HttpPost(url);
//        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();
//        httpPost.setConfig(requestConfig);
//
//        StringEntity postEntity = new StringEntity(data, "UTF-8");
//        httpPost.addHeader("Content-Type", "text/xml");
////      httpPost.addHeader("User-Agent", USER_AGENT + " " + config.getMchID());
//        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        System.out.println("httpResponse : " + httpResponse.toString());
        HttpEntity httpEntity = httpResponse.getEntity();
        return httpEntity;

    }


    /**
     * @Description: 获取票据的接口
     * @Author: mac
     * @Date: 2024/9/24 16:11
     * @param accessToken:
     * @param sceneString:
     * @return: java.lang.String
     *
     */
    public static String getTickets(String accessToken, String sceneString) throws IOException {

        if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(sceneString)) {
            return "error";
        }

        BasicHttpClientConnectionManager connManager = null;
        if (false) {
        }
        else {
            connManager = new BasicHttpClientConnectionManager(
                    RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
                            .register("https", SSLConnectionSocketFactory.getSocketFactory())
                            .build(),
                    null,
                    null,
                    null
            );
        }

        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(connManager)
                .build();

        String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken;

        HttpPost httpPost = new HttpPost(url);
        int readTimeoutMs = 3000;
        int connectTimeoutMs = 3000;
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();
        httpPost.setConfig(requestConfig);

        StringEntity postEntity = new StringEntity(sceneString, "UTF-8");
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        System.out.println("httpResponse : " + httpResponse.toString());
        HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity, "UTF-8");


    }
}
